package cn.dankal.flyaudio.personal.brand

import android.app.Activity
import android.content.Intent
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.View
import api.CurrentServiceFactory
import cn.dankal.basiclib.base.activity.BaseStateActivity
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener
import cn.dankal.basiclib.rx.AbstractStateSubscriber
import cn.dankal.slidebarlib.BrandBean
import cn.dankal.business.pojo.BrandResponse
import cn.dankal.flyaudio.R
import cn.dankal.slidebarlib.SectionItemDecoration
import cn.dankal.slidebarlib.SideIndexBar
import kotlinx.android.synthetic.main.activity_brand.*
import org.json.JSONArray
import org.json.JSONObject

class BrandActivity : BaseStateActivity() {
    override fun contentView(): Any {
        return rl_content
    }

    private lateinit var mAdapter:BrandAdapter


    override fun getLayoutId(): Int {
        return R.layout.activity_brand
    }

    override fun initComponents() {

        addSingleTitleBar("品牌")
        cp_side_index_bar.setOverlayTextView(tv_cp_overlay)
        cp_side_index_bar.setOnIndexChangedListener(object :SideIndexBar.OnIndexTouchedChangedListener{
            override fun onIndexChanged(index: String, position: Int) {
                mAdapter.scrollToSection(index)
            }
        })

       var mLayoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        cp_city_recyclerview.setLayoutManager(mLayoutManager)
        cp_city_recyclerview.setHasFixedSize(true)

        CurrentServiceFactory.brand_list()
                .subscribe(object :AbstractStateSubscriber<com.alibaba.fastjson.JSONObject>(this){
                    override fun onNext(t: com.alibaba.fastjson.JSONObject) {
                        super.onNext(t)

                        var listOf = mutableListOf<BrandBean>()

                        t.keys.iterator().forEach {
                            var list=com.alibaba.fastjson.JSONArray.parseArray(t.getJSONArray(it).toJSONString(),BrandBean::class.java)
                            for (brandBean in list) {
                                brandBean.pinyin=it
                            }
                            listOf.addAll(list)
                        }

                        cp_city_recyclerview.addItemDecoration(SectionItemDecoration(this@BrandActivity, listOf), 0)
                        cp_city_recyclerview.addItemDecoration(DividerItemDecoration(this@BrandActivity,DividerItemDecoration.VERTICAL), 1)
                        mAdapter=BrandAdapter(mLayoutManager)
                        mAdapter.setOnRvItemClickListener(object: OnRvItemClickListener<BrandBean>() {
                            override fun onItemClick(v: View?, position: Int, data: BrandBean) {

                                var intent=Intent()
                                intent.putExtra("brand",data.brandName)
                                intent.putExtra("brandId",data.uuid)
                                setResult( Activity.RESULT_OK,intent)
                                finish()
                            }

                        })
                        cp_city_recyclerview.setAdapter(mAdapter)
                        mAdapter.updateData(listOf)
                    }
                })
    }
}
